<?php

namespace App\Services;

use App\Exceptions\BusinessException;
use App\Models\Activity;
use App\Models\BaseModel;

class ActivityService extends BaseService
{
    /**
     * 活动列表
     * @param $page
     * @param $pageSize
     * @param $keyword
     * @param $state
     * @return array
     */
    public function list($page, $pageSize, $keyword, $state)
    {
        // 设置对象
        $model = Activity::query();
        // 关键词
        if ($keyword) {
            $model = $model->where('activity_name', 'like', '%' . $keyword . '%');
        }
        // 状态
        if ($state) {
            if ($state == Activity::STATE_WAIT) {
                $model = $model->where('start_time', '>', date('Y-m-d'));
            }
            if ($state == Activity::STATE_ING) {
                $model = $model->where('start_time', '<', date('Y-m-d'))
                    ->where('end_time', '>', date('Y-m-d'));
            }
            if ($state == Activity::STATE_END) {
                $model = $model->where('end_time', '<', date('Y-m-d'));
            }
        }
        // 状态
        $model = $model->where('status', BaseModel::STATUS_ENABLE);
        // 获取列表
        $result = $model
            ->orderBy('sort', 'desc')
            ->orderBy('id', 'desc')
            ->paginate($pageSize, ['id', 'activity_name', 'activity_img', 'start_time', 'end_time'], 'page', $page);

        $now = time();
        $result->getCollection()->transform(function ($item) use ($now) {
            $item = json_decode($item, true);
            $startTime = strtotime($item['start_time']);
            $endTime = strtotime($item['end_time']);
            $item['state'] = Activity::STATE_END;
            if ($now < $startTime) {
                $item['state'] = Activity::STATE_WAIT;
            } elseif ($now > $startTime && $now < $endTime) {
                $item['state'] = Activity::STATE_ING;
            }
            $item['activity_img'] = storageFirst($item['activity_img']);
            $item['start_time'] = date('Y/m/d', $startTime);
            $item['end_time'] = date('Y/m/d', $endTime);
            return $item;
        });

        return $this->successPage($result);
    }

    /**
     * 活动详情
     * @param $activityId
     * @return array
     * @throws BusinessException
     */
    public function detail($activityId)
    {
        // 获取数据
        $data = Activity::query()->where('id', $activityId)->first();
        if (!$data) {
            $this->throwBusinessException('活动不存在');
        }
        $data->increment('views');
        $data = $data->toArray();

        $data['activity_img'] = storageMulti($data['activity_img']);
        // 时间
        $now = time();
        $startTime = strtotime($data['start_time']);
        $endTime = strtotime($data['end_time']);
        $data['state'] = Activity::STATE_END;
        if ($now < $startTime) {
            $data['state'] = Activity::STATE_WAIT;
        } elseif ($now > $startTime && $now < $endTime) {
            $data['state'] = Activity::STATE_ING;
        }
        $data['start_time'] = date('Y/m/d', $startTime);
        $data['end_time'] = date('Y/m/d', $endTime);
        return $data;
    }
}
